package com.qlmy.cn.ui.adapter

import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import com.qlmy.cn.bean.ImageUploadBean
import com.qlmy.cn.ui.base.BaseRecyclerAdapter
import com.qlmy.cn.ui.base.BaseViewHolder
import com.qlmy.cn.utils.GlideUtils
import com.qlmy.cn.utils.ImageUploadUtils
import com.qlmy.cn.utils.selectImage
import com.wbs.shop.nuby.R
import com.wbs.shop.nuby.databinding.ItemProductImageBinding

/**
 * 图片选择
 */
class UserImageAdapter(
    var value: Int = -1,
    private val defaultUpImage: Int = R.mipmap.icon_edit_user_default_up_image,
    fragment: Fragment? = null,
    activity: FragmentActivity? = null,
    imageUploadUtils: ImageUploadUtils? = null,
    private val changeCallback: (() -> Unit)? = null
) : BaseRecyclerAdapter<BaseViewHolder<ItemProductImageBinding>, ImageUploadBean>() {
    var maxLength: Int = 6
    private var upImageBean: ImageUploadBean = ImageUploadBean().apply { resId = defaultUpImage }// 默认一个站位图
    private var mImageUploadUtils: ImageUploadUtils? = null

    init {
        newDatas(datas)
        if (fragment != null) {
            mImageUploadUtils = ImageUploadUtils(fragment, this::onUploadSuccess)
        }
        if (activity != null) {
            mImageUploadUtils = ImageUploadUtils(activity, this::onUploadSuccess)
        }
        if (imageUploadUtils != null)
            mImageUploadUtils = imageUploadUtils
    }

    override fun newDatas(datas: MutableList<ImageUploadBean>) {
        if (datas.size < maxLength && !datas.contains(upImageBean)) datas.add(upImageBean)
    }

    fun addImage(albumFiles: MutableList<ImageUploadBean>) {
        if (albumFiles.isEmpty()) return
        val indexOf = datas.indexOf(upImageBean)
        datas.remove(upImageBean)
        notifyItemRemoved(indexOf)
        datas.addAll(albumFiles)
        val position = datas.size - 1
        if (datas.size < maxLength) datas.add(upImageBean)
        notifyItemRangeChanged(position, datas.size - position)
    }

    fun getOriginalSize(): Int {
        return getGalleryAlbums().size
    }

    fun getGalleryAlbums(): ArrayList<String> {
        val newDatas = ArrayList(datas).apply { remove(upImageBean) }
        val albumFiles: ArrayList<String> = ArrayList()
        for (item in newDatas) {
            albumFiles.add(item.url ?: item.filePath ?: "")
        }
        return albumFiles
    }

    fun getFiles(): ArrayList<ImageUploadBean> = ArrayList(datas).apply { remove(upImageBean) }

    override fun remove(position: Int) {
        super.remove(position)
        if (datas.size < maxLength && !datas.contains(upImageBean)) {
            datas.add(upImageBean)
            notifyItemInserted(datas.size - 1)
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, p1: Int) = BaseViewHolder(ItemProductImageBinding.inflate(getLayoutInflate(parent.context), parent, false))

    override fun onBindViewHolder(holder: BaseViewHolder<ItemProductImageBinding>, bean: ImageUploadBean, position: Int) {
        holder.mViewBinding.run {
            if (bean.resId > 0) {
                GlideUtils.intoView(ivImage.context, bean.filePath ?: bean.url ?: bean.resId, ivImage)
            } else {
                GlideUtils.intoRoundedCorners(ivImage.context, bean.filePath ?: bean.url ?: bean.resId, ivImage, roundingRadius = 10)
            }
            tvDelete.visibility = if (bean.resId > 0) View.GONE else View.VISIBLE
            setViewClick(position, ivImage, tvDelete)
            setViewClickValue(value, ivImage, tvDelete)
        }
    }

    override fun subOnClickAdapter(view: View, position: Int) {
        if (mImageUploadUtils != null) {
            selectImage(view, position, mImageUploadUtils)
        }
    }

    fun selectImage(view: View, position: Int, mImageUploadUtils: ImageUploadUtils?) {
        val item = getItem(position)
        when (view.id) {
            R.id.ivImage -> {
                if (item.resId > 0) {// 点击的是添加图片
                    mImageUploadUtils?.selectImage { selectCount = maxLength - getOriginalSize() }
                } else {
                    mImageUploadUtils?.gallery(getGalleryAlbums(), position)
                }
            }
            R.id.tvDelete -> {
                remove(position)
            }
        }
    }

    private fun onUploadSuccess(imageUploads: MutableList<ImageUploadBean>) {
        addImage(imageUploads)
        changeCallback?.invoke()
    }
}